package com.cnb.telepathytalk.packet;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.Serializable;
import java.net.Socket;
import java.util.Arrays;

import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.cnb.telepathytalk.IntroActivity;
import com.cnb.telepathytalk.TelepathyTalkApplication;
import com.cnb.telepathytalk.packet.pt01.Packet_PT1_ST1;
import com.cnb.telepathytalk.packet.pt01.Packet_PT1_ST2;
import com.cnb.telepathytalk.packet.pt01.Packet_PT1_ST3;
import com.cnb.telepathytalk.packet.pt01.Packet_PT1_ST4;
import com.cnb.telepathytalk.packet.pt01.Packet_PT1_ST5;
import com.cnb.telepathytalk.packet.pt01.Packet_PT1_ST7;
import com.cnb.telepathytalk.packet.pt01.Packet_PT1_ST8;
import com.cnb.telepathytalk.packet.pt02.Packet_PT2_ST01;
import com.cnb.telepathytalk.packet.pt02.Packet_PT2_ST02;
import com.cnb.telepathytalk.packet.pt02.Packet_PT2_ST03;
import com.cnb.telepathytalk.packet.pt02.Packet_PT2_ST04;
import com.cnb.telepathytalk.packet.pt02.Packet_PT2_ST05;
import com.cnb.telepathytalk.packet.pt02.Packet_PT2_ST10;
import com.cnb.telepathytalk.packet.pt02.Packet_PT2_ST11;
import com.cnb.telepathytalk.packet.pt02.Packet_PT2_ST12;
import com.cnb.telepathytalk.packet.pt02.Packet_PT2_ST20;
import com.cnb.telepathytalk.packet.pt02.Packet_PT2_ST21;
import com.cnb.telepathytalk.packet.pt02.Packet_PT2_ST22;
import com.cnb.telepathytalk.packet.pt02.Packet_PT2_ST30;
import com.cnb.telepathytalk.packet.pt02.Packet_PT2_ST31;
import com.cnb.telepathytalk.packet.pt05.Packet_PT5_ST1;
import com.cnb.telepathytalk.packet.pt05.Packet_PT5_ST2;
import com.cnb.telepathytalk.packet.pt06.Packet_PT6_ST01;
import com.cnb.telepathytalk.packet.pt06.Packet_PT6_ST02;
import com.cnb.telepathytalk.packet.pt06.Packet_PT6_ST03;
import com.cnb.telepathytalk.packet.pt06.Packet_PT6_ST04;
import com.cnb.telepathytalk.packet.pt06.Packet_PT6_ST05;
import com.cnb.telepathytalk.packet.pt06.Packet_PT6_ST06;
import com.cnb.telepathytalk.packet.pt06.Packet_PT6_ST07;
import com.cnb.telepathytalk.packet.pt06.Packet_PT6_ST08;
import com.cnb.telepathytalk.packet.pt06.Packet_PT6_ST100;
import com.cnb.telepathytalk.packet.pt06.Packet_PT6_ST101;
import com.cnb.telepathytalk.packet.pt07.Packet_PT7_ST01;
import com.cnb.telepathytalk.packet.pt08.Packet_PT8_ST01;
import com.cnb.telepathytalk.packet.pt08.Packet_PT8_ST02;
import com.cnb.telepathytalk.packet.pt08.Packet_PT8_ST04;
import com.cnb.telepathytalk.packet.pt08.Packet_PT8_ST05;
import com.cnb.telepathytalk.packet.pt08.Packet_PT8_ST06;
import com.cnb.telepathytalk.packet.pt08.Packet_PT8_ST07;
import com.cnb.telepathytalk.packet.pt08.Packet_PT8_ST11;
import com.cnb.telepathytalk.packet.pt08.Packet_PT8_ST13;
import com.cnb.telepathytalk.packet.pt08.Packet_PT8_ST14;
import com.cnb.telepathytalk.packet.pt08.Packet_PT8_ST23;
import com.cnb.telepathytalk.packet.pt08.Packet_PT8_ST24;
import com.cnb.telepathytalk.packet.pt08.Packet_PT8_ST25;
import com.cnb.telepathytalk.packet.pt08.Packet_PT8_ST26;
import com.cnb.telepathytalk.packet.pt08.Packet_PT8_ST28;
import com.cnb.telepathytalk.packet.pt08.Packet_PT8_ST31;
import com.cnb.telepathytalk.packet.pt08.Packet_PT8_ST32;
import com.cnb.telepathytalk.packet.pt08.Packet_PT8_ST34;
import com.cnb.telepathytalk.packet.pt08.Packet_PT8_ST35;
import com.cnb.telepathytalk.packet.pt08.Packet_PT8_ST36;
import com.cnb.telepathytalk.packet.pt08.Packet_PT8_ST37;
import com.cnb.telepathytalk.packet.pt08.Packet_PT8_ST38;
import com.cnb.telepathytalk.packet.pt08.Packet_PT8_ST39;
import com.cnb.telepathytalk.packet.pt08.Packet_PT8_ST40;
import com.cnb.telepathytalk.packet.pt08.Packet_PT8_ST44;
import com.cnb.telepathytalk.packet.pt08.Packet_PT8_ST50;
import com.cnb.telepathytalk.packet.pt08.Packet_PT8_ST51;
import com.cnb.telepathytalk.packet.pt08.Packet_PT8_ST52;
import com.cnb.telepathytalk.packet.pt08.Packet_PT8_ST53;
import com.cnb.telepathytalk.packet.pt10.Packet_PT10_ST02;
import com.cnb.telepathytalk.packet.pt10.Packet_PT10_ST03;
import com.cnb.telepathytalk.packet.pt10.Packet_PT10_ST04;
import com.cnb.telepathytalk.packet.pt10.Packet_PT10_ST10;
import com.cnb.telepathytalk.packet.pt10.Packet_PT10_ST11;
import com.cnb.telepathytalk.packet.pt10.Packet_PT10_ST20;
import com.cnb.telepathytalk.packet.pt10.Packet_PT10_ST21;
import com.cnb.telepathytalk.packet.pt10.Packet_PT10_ST30;
import com.cnb.telepathytalk.packet.pt10.Packet_PT10_ST31;
import com.cnb.telepathytalk.packet.pt10.Packet_PT10_ST40;
import com.cnb.telepathytalk.packet.pt10.Packet_PT10_ST42;
import com.cnb.telepathytalk.packet.pt10.Packet_PT10_ST43;
import com.cnb.telepathytalk.packet.pt10.Packet_PT10_ST44;
import com.cnb.telepathytalk.packet.pt10.Packet_PT10_ST50;
import com.cnb.telepathytalk.packet.pt10.Packet_PT10_ST51;
import com.cnb.telepathytalk.packet.pt10.Packet_PT10_ST58;
import com.cnb.telepathytalk.packet.pt10.Packet_PT10_ST59;
import com.cnb.telepathytalk.packet.pt10.Packet_PT10_ST71;
import com.cnb.telepathytalk.packet.pt10.Packet_PT10_ST72;
import com.cnb.telepathytalk.packet.pt11.Packet_PT11_ST01;
import com.cnb.telepathytalk.packet.pt11.Packet_PT11_ST02;
import com.cnb.telepathytalk.packet.pt11.Packet_PT11_ST03;
import com.cnb.telepathytalk.packet.pt11.Packet_PT11_ST04;
import com.cnb.telepathytalk.packet.pt12.Packet_PT12_ST01;
import com.cnb.telepathytalk.packet.pt12.Packet_PT12_ST02;

public class TelepathyPacket implements InterfacePacket, Serializable {
	private static final String TAG = "TelepathyPacket";
	private static final long serialVersionUID = 5330178691021676830L;

	private PacketHeader header;
	private InterfacePacketBody body;

	public TelepathyPacket(byte[] data) {
		super();
		header = new PacketHeader(Arrays.copyOfRange(data, 0, PacketHeader.SIZE));

		int type = header.getIntPacketType();

		// PT1
		if (type == Packet_PT1_ST1.ID_INT)
			body = new Packet_PT1_ST1(Arrays.copyOfRange(data, PacketHeader.SIZE, data.length));

		else if (type == Packet_PT1_ST2.ID_INT)
			body = new Packet_PT1_ST2();

		else if (type == Packet_PT1_ST3.ID_INT)
			body = new Packet_PT1_ST3(Arrays.copyOfRange(data, PacketHeader.SIZE, data.length));

		else if (type == Packet_PT1_ST4.ID_INT)
			body = new Packet_PT1_ST4(Arrays.copyOfRange(data, PacketHeader.SIZE, data.length));

		else if (type == Packet_PT1_ST5.ID_INT)
			body = new Packet_PT1_ST5();

		else if (type == Packet_PT1_ST7.ID_INT)
			body = new Packet_PT1_ST7();

		else if (type == Packet_PT1_ST8.ID_INT)
			body = new Packet_PT1_ST8();

		// PT2
		else if (type == Packet_PT2_ST01.ID_INT)
			body = new Packet_PT2_ST01(Arrays.copyOfRange(data, PacketHeader.SIZE, data.length));

		else if (type == Packet_PT2_ST02.ID_INT)
			body = new Packet_PT2_ST02(Arrays.copyOfRange(data, PacketHeader.SIZE, data.length));

		else if (type == Packet_PT2_ST03.ID_INT)
			body = new Packet_PT2_ST03(Arrays.copyOfRange(data, PacketHeader.SIZE, data.length));

		else if (type == Packet_PT2_ST04.ID_INT)
			body = new Packet_PT2_ST04(Arrays.copyOfRange(data, PacketHeader.SIZE, data.length));

		else if (type == Packet_PT2_ST05.ID_INT)
			body = new Packet_PT2_ST05(Arrays.copyOfRange(data, PacketHeader.SIZE, data.length));

		else if (type == Packet_PT2_ST10.ID_INT)
			body = new Packet_PT2_ST10(Arrays.copyOfRange(data, PacketHeader.SIZE, data.length));

		else if (type == Packet_PT2_ST11.ID_INT)
			body = new Packet_PT2_ST11(Arrays.copyOfRange(data, PacketHeader.SIZE, data.length));

		else if (type == Packet_PT2_ST12.ID_INT)
			body = new Packet_PT2_ST12(Arrays.copyOfRange(data, PacketHeader.SIZE, data.length));

		else if (type == Packet_PT2_ST20.ID_INT)
			body = new Packet_PT2_ST20(Arrays.copyOfRange(data, PacketHeader.SIZE, data.length));

		else if (type == Packet_PT2_ST21.ID_INT)
			body = new Packet_PT2_ST21(Arrays.copyOfRange(data, PacketHeader.SIZE, data.length));

		else if (type == Packet_PT2_ST22.ID_INT)
			body = new Packet_PT2_ST22(Arrays.copyOfRange(data, PacketHeader.SIZE, data.length));

		else if (type == Packet_PT2_ST30.ID_INT)
			body = new Packet_PT2_ST30(Arrays.copyOfRange(data, PacketHeader.SIZE, data.length));

		else if (type == Packet_PT2_ST31.ID_INT)
			body = new Packet_PT2_ST31(Arrays.copyOfRange(data, PacketHeader.SIZE, data.length));

		// PT5
		else if (type == Packet_PT5_ST1.ID_INT)
			body = new Packet_PT5_ST1(Arrays.copyOfRange(data, PacketHeader.SIZE, data.length));

		else if (type == Packet_PT5_ST2.ID_INT)
			body = new Packet_PT5_ST2(Arrays.copyOfRange(data, PacketHeader.SIZE, data.length));

		// PT6
		else if (type == Packet_PT6_ST01.ID_INT)
			body = new Packet_PT6_ST01(Arrays.copyOfRange(data, PacketHeader.SIZE, data.length));

		else if (type == Packet_PT6_ST02.ID_INT)
			body = new Packet_PT6_ST02(Arrays.copyOfRange(data, PacketHeader.SIZE, data.length));

		else if (type == Packet_PT6_ST03.ID_INT)
			body = new Packet_PT6_ST03(Arrays.copyOfRange(data, PacketHeader.SIZE, data.length));

		else if (type == Packet_PT6_ST04.ID_INT)
			body = new Packet_PT6_ST04(Arrays.copyOfRange(data, PacketHeader.SIZE, data.length));

		else if (type == Packet_PT6_ST05.ID_INT)
			body = new Packet_PT6_ST05(Arrays.copyOfRange(data, PacketHeader.SIZE, data.length));

		else if (type == Packet_PT6_ST06.ID_INT)
			body = new Packet_PT6_ST06(Arrays.copyOfRange(data, PacketHeader.SIZE, data.length));

		else if (type == Packet_PT6_ST07.ID_INT)
			body = new Packet_PT6_ST07(Arrays.copyOfRange(data, PacketHeader.SIZE, data.length));

		else if (type == Packet_PT6_ST08.ID_INT)
			body = new Packet_PT6_ST08(Arrays.copyOfRange(data, PacketHeader.SIZE, data.length));

		// else if (type == Packet_PT6_ST10.ID_INT)
		// body = new Packet_PT6_ST010(Arrays.copyOfRange(data,
		// PacketHeader.SIZE, data.length));
		//
		// else if (type == Packet_PT6_ST11.ID_INT)
		// body = new Packet_PT6_ST11(Arrays.copyOfRange(data,
		// PacketHeader.SIZE, data.length));
		//
		// else if (type == Packet_PT6_ST12.ID_INT)
		// body = new Packet_PT6_ST12(Arrays.copyOfRange(data,
		// PacketHeader.SIZE, data.length));

		else if (type == Packet_PT6_ST100.ID_INT)
			body = new Packet_PT6_ST100(Arrays.copyOfRange(data, PacketHeader.SIZE, data.length));

		else if (type == Packet_PT6_ST101.ID_INT)
			body = new Packet_PT6_ST101(Arrays.copyOfRange(data, PacketHeader.SIZE, data.length));

		// PT7
		else if (type == Packet_PT7_ST01.ID_INT)
			body = new Packet_PT7_ST01(Arrays.copyOfRange(data, PacketHeader.SIZE, data.length));

		// PT8
		else if (type == Packet_PT8_ST01.ID_INT)
			body = new Packet_PT8_ST01(Arrays.copyOfRange(data, PacketHeader.SIZE, data.length));

		else if (type == Packet_PT8_ST02.ID_INT)
			body = new Packet_PT8_ST02(Arrays.copyOfRange(data, PacketHeader.SIZE, data.length));

		else if (type == Packet_PT8_ST04.ID_INT)
			body = new Packet_PT8_ST04(Arrays.copyOfRange(data, PacketHeader.SIZE, data.length));

		else if (type == Packet_PT8_ST05.ID_INT)
			body = new Packet_PT8_ST05(Arrays.copyOfRange(data, PacketHeader.SIZE, data.length));

		else if (type == Packet_PT8_ST06.ID_INT)
			body = new Packet_PT8_ST06(Arrays.copyOfRange(data, PacketHeader.SIZE, data.length));

		else if (type == Packet_PT8_ST07.ID_INT)
			body = new Packet_PT8_ST07(Arrays.copyOfRange(data, PacketHeader.SIZE, data.length));

		else if (type == Packet_PT8_ST11.ID_INT)
			body = new Packet_PT8_ST11(Arrays.copyOfRange(data, PacketHeader.SIZE, data.length));

		else if (type == Packet_PT8_ST13.ID_INT)
			body = new Packet_PT8_ST13(Arrays.copyOfRange(data, PacketHeader.SIZE, data.length));

		else if (type == Packet_PT8_ST14.ID_INT)
			body = new Packet_PT8_ST14(Arrays.copyOfRange(data, PacketHeader.SIZE, data.length));

		else if (type == Packet_PT8_ST23.ID_INT)
			body = new Packet_PT8_ST23(Arrays.copyOfRange(data, PacketHeader.SIZE, data.length));

		else if (type == Packet_PT8_ST24.ID_INT)
			body = new Packet_PT8_ST24(Arrays.copyOfRange(data, PacketHeader.SIZE, data.length));

		else if (type == Packet_PT8_ST25.ID_INT)
			body = new Packet_PT8_ST25(Arrays.copyOfRange(data, PacketHeader.SIZE, data.length));

		else if (type == Packet_PT8_ST26.ID_INT)
			body = new Packet_PT8_ST26(Arrays.copyOfRange(data, PacketHeader.SIZE, data.length));

		else if (type == Packet_PT8_ST28.ID_INT)
			body = new Packet_PT8_ST28(Arrays.copyOfRange(data, PacketHeader.SIZE, data.length));

		else if (type == Packet_PT8_ST31.ID_INT)
			body = new Packet_PT8_ST31(Arrays.copyOfRange(data, PacketHeader.SIZE, data.length));

		else if (type == Packet_PT8_ST32.ID_INT)
			body = new Packet_PT8_ST32(Arrays.copyOfRange(data, PacketHeader.SIZE, data.length));

		else if (type == Packet_PT8_ST34.ID_INT)
			body = new Packet_PT8_ST34(Arrays.copyOfRange(data, PacketHeader.SIZE, data.length));

		else if (type == Packet_PT8_ST35.ID_INT)
			body = new Packet_PT8_ST35(Arrays.copyOfRange(data, PacketHeader.SIZE, data.length));

		else if (type == Packet_PT8_ST36.ID_INT)
			body = new Packet_PT8_ST36(Arrays.copyOfRange(data, PacketHeader.SIZE, data.length));

		else if (type == Packet_PT8_ST37.ID_INT)
			body = new Packet_PT8_ST37(Arrays.copyOfRange(data, PacketHeader.SIZE, data.length));

		else if (type == Packet_PT8_ST38.ID_INT)
			body = new Packet_PT8_ST38(Arrays.copyOfRange(data, PacketHeader.SIZE, data.length));

		else if (type == Packet_PT8_ST39.ID_INT)
			body = new Packet_PT8_ST39(Arrays.copyOfRange(data, PacketHeader.SIZE, data.length));

		else if (type == Packet_PT8_ST40.ID_INT)
			body = new Packet_PT8_ST40(Arrays.copyOfRange(data, PacketHeader.SIZE, data.length));

		else if (type == Packet_PT8_ST44.ID_INT)
			body = new Packet_PT8_ST44(Arrays.copyOfRange(data, PacketHeader.SIZE, data.length));

		else if (type == Packet_PT8_ST50.ID_INT)
			body = new Packet_PT8_ST50(Arrays.copyOfRange(data, PacketHeader.SIZE, data.length));

		else if (type == Packet_PT8_ST51.ID_INT)
			body = new Packet_PT8_ST51(Arrays.copyOfRange(data, PacketHeader.SIZE, data.length));

		else if (type == Packet_PT8_ST52.ID_INT)
			body = new Packet_PT8_ST52(Arrays.copyOfRange(data, PacketHeader.SIZE, data.length));

		else if (type == Packet_PT8_ST53.ID_INT)
			body = new Packet_PT8_ST53(Arrays.copyOfRange(data, PacketHeader.SIZE, data.length));

		// PT10
		else if (type == Packet_PT10_ST02.ID_INT)
			body = new Packet_PT10_ST02(Arrays.copyOfRange(data, PacketHeader.SIZE, data.length));

		else if (type == Packet_PT10_ST03.ID_INT)
			body = new Packet_PT10_ST03(Arrays.copyOfRange(data, PacketHeader.SIZE, data.length));

		else if (type == Packet_PT10_ST04.ID_INT)
			body = new Packet_PT10_ST04(Arrays.copyOfRange(data, PacketHeader.SIZE, data.length));

		else if (type == Packet_PT10_ST10.ID_INT)
			body = new Packet_PT10_ST10(Arrays.copyOfRange(data, PacketHeader.SIZE, data.length));

		else if (type == Packet_PT10_ST11.ID_INT)
			body = new Packet_PT10_ST11(Arrays.copyOfRange(data, PacketHeader.SIZE, data.length));

		else if (type == Packet_PT10_ST20.ID_INT)
			body = new Packet_PT10_ST20(Arrays.copyOfRange(data, PacketHeader.SIZE, data.length));

		else if (type == Packet_PT10_ST21.ID_INT)
			body = new Packet_PT10_ST21(Arrays.copyOfRange(data, PacketHeader.SIZE, data.length));

		else if (type == Packet_PT10_ST30.ID_INT)
			body = new Packet_PT10_ST30(Arrays.copyOfRange(data, PacketHeader.SIZE, data.length));

		else if (type == Packet_PT10_ST31.ID_INT)
			body = new Packet_PT10_ST31(Arrays.copyOfRange(data, PacketHeader.SIZE, data.length));

		else if (type == Packet_PT10_ST40.ID_INT)
			body = new Packet_PT10_ST40(Arrays.copyOfRange(data, PacketHeader.SIZE, data.length));

		else if (type == Packet_PT10_ST42.ID_INT)
			body = new Packet_PT10_ST42(Arrays.copyOfRange(data, PacketHeader.SIZE, data.length));

		else if (type == Packet_PT10_ST43.ID_INT)
			body = new Packet_PT10_ST43(Arrays.copyOfRange(data, PacketHeader.SIZE, data.length));

		else if (type == Packet_PT10_ST44.ID_INT)
			body = new Packet_PT10_ST44(Arrays.copyOfRange(data, PacketHeader.SIZE, data.length));

		else if (type == Packet_PT10_ST50.ID_INT)
			body = new Packet_PT10_ST50(Arrays.copyOfRange(data, PacketHeader.SIZE, data.length));

		else if (type == Packet_PT10_ST51.ID_INT)
			body = new Packet_PT10_ST51(Arrays.copyOfRange(data, PacketHeader.SIZE, data.length));

		else if (type == Packet_PT10_ST58.ID_INT)
			body = new Packet_PT10_ST58(Arrays.copyOfRange(data, PacketHeader.SIZE, data.length));

		else if (type == Packet_PT10_ST59.ID_INT)
			body = new Packet_PT10_ST59(Arrays.copyOfRange(data, PacketHeader.SIZE, data.length));

		else if (type == Packet_PT10_ST71.ID_INT)
			body = new Packet_PT10_ST71(Arrays.copyOfRange(data, PacketHeader.SIZE, data.length));

		else if (type == Packet_PT10_ST72.ID_INT)
			body = new Packet_PT10_ST72(Arrays.copyOfRange(data, PacketHeader.SIZE, data.length));

		// PT11
		else if (type == Packet_PT11_ST01.ID_INT)
			body = new Packet_PT11_ST01();

		else if (type == Packet_PT11_ST02.ID_INT)
			body = new Packet_PT11_ST02(Arrays.copyOfRange(data, PacketHeader.SIZE, data.length));

		else if (type == Packet_PT11_ST03.ID_INT)
			body = new Packet_PT11_ST03(Arrays.copyOfRange(data, PacketHeader.SIZE, data.length));

		else if (type == Packet_PT11_ST04.ID_INT)
			body = new Packet_PT11_ST04(Arrays.copyOfRange(data, PacketHeader.SIZE, data.length));

		// PT12
		else if (type == Packet_PT12_ST01.ID_INT)
			body = new Packet_PT12_ST01();

		else if (type == Packet_PT12_ST02.ID_INT)
			body = new Packet_PT12_ST02(Arrays.copyOfRange(data, PacketHeader.SIZE, data.length));

	}

	public TelepathyPacket(PacketHeader header, InterfacePacketBody body) {
		super();
		this.header = header;
		this.body = body;
	}

	public void threadSendSocket(final Context context) {
		new Thread() {
			public void run() {
				Socket socket = null;
				try {
					Log.i(TAG, "Packe Size : " + getHeader().getPacketSize());
					Log.i(TAG, "Packe Send Count : " + getByte().length);
					Log.i(TAG, "Packet Send : " + body.getStringPacketType());

					socket = TelepathyTalkApplication.getSocket(false);
					DataOutputStream dos = new DataOutputStream(socket.getOutputStream());

					dos.write(Arrays.copyOfRange(getByte(), 0, getByte().length));

				} catch (Exception e) {
					Log.e(TAG, e.toString());
					e.printStackTrace();
					try {
						if (socket != null) {
							socket.close();
						}
						Intent intent = new Intent(context, IntroActivity.class);
						intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
						context.startActivity(intent);
					} catch (IOException e1) {
						e1.printStackTrace();
					}
				}
			}
		}.start();
	}

	public PacketHeader getHeader() {
		return header;
	}

	public InterfacePacketBody getBody() {
		return body;
	}

	@Override
	public int getIntPacketType() {
		return header.getIntPacketType();
	}

	@Override
	public String getStringPacketType() {
		return header.getStringPacketType();
	}

	@Override
	public byte[] getByte() {
		return TelepathyPacketUtil.getTotalByte(header.getByte(), body.getByte());
	}
}